package co.crossoft.daoImpl;

import co.crossoft.base.common.JsonResult;
import co.crossoft.domain.CommonDialog;
import co.crossoft.domain.ResourceWorkCenter;
import co.crossoft.domain.WorkCenter;
import co.crossoft.mapper.WorkCenterMapper;
import co.crossoft.util.JsonConvertUtil;
import co.crossoft.util.JsonReturnMsg;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Snow on 8/22/2017.
 */
@Repository("workCenterDaoImpl")
public class WorkCenterDaoImpl {

    @Autowired
    WorkCenterMapper workCenterMapper;

    public JsonResult addOrUpdateWorkCenter(WorkCenter workCenter) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (workCenter.getPlant().equals("") || workCenter.getWorkCenter().equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("required column can not be empty!");
        } else {
            Map parMap = new HashMap();
            parMap.put("plant", workCenter.getPlant());
            parMap.put("workCenter", workCenter.getWorkCenter());
            List list = workCenterMapper.workCenterIsExist(parMap);
            if (list.size() > 0) {
                workCenterMapper.updateWorkCenter(workCenter);
            } else {
                workCenterMapper.addWorkCenter(workCenter);
            }
        }
        return jsonResult;
    }

    public JsonResult selectOneworkCenterByHandle(String handle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (handle.equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("search index parameter can not be null ,please try to select workCenter before!");
        } else {
            List workCenterlist = workCenterMapper.selectOneWorkCenterByHandle(handle);
            Map<String, List> retMap = new HashMap();
            if (workCenterlist.size() > 0) {
                List workResList = workCenterMapper.selectResourceRefWorkCenter(handle);
                retMap.put("workCenterList", workCenterlist);
                retMap.put("workResList", workResList);
                jsonResult.setResult(retMap);
            } else {
                jsonResult.setResultCode("F");
                jsonResult.setResultMsg("no data can be founded!");
            }
        }
        return jsonResult;
    }

    public JsonResult deleteWorkCenter(String handle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (handle.equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("work center unique identification can not be empty!");
        } else {
            workCenterMapper.deleteByPrimaryKey(handle);
        }
        return jsonResult;
    }

    public JsonResult getWorkCenterList(String plant, String workCenter) {
        JsonResult jsonResult = new JsonResult("S","success");
        Map<String, String> parMap = new HashMap();
        parMap.put("plant", plant);
        parMap.put("workCenter", workCenter);
        List<WorkCenter> list = workCenterMapper.selectByWorkCenter(parMap);
        jsonResult.setResult(list);
        return jsonResult;
    }

    public JsonResult workCenterOrResourceSelect(CommonDialog commonDialog) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (commonDialog.getPlant().equals("") || commonDialog.getType().equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("required parameter can not be empty!");
        } else {
            List list = workCenterMapper.workCenterOrResourceSelect(commonDialog);
            jsonResult.setResult(list);
        }
        return jsonResult;
    }

    public JsonResult deleteWorkRefResource(ArrayList<String> handleData, String workCenterHandle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (handleData.size() <= 0) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("delete unique identity can not be empty!");
        } else {
            for (int i = 0; i < handleData.size(); i++) {
                String oneHandle = handleData.get(i);
                workCenterMapper.deleteWorkCenterRefResource(oneHandle);
            }
            List workRefResList = workCenterMapper.selectResourceRefWorkCenter(workCenterHandle);
            jsonResult.setResult(workRefResList);
        }
        return jsonResult;
    }

    public JsonResult addWorkCenterRefResource(ResourceWorkCenter resourceWorkCenter) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (resourceWorkCenter.getResourceOrWorkcenterFk().equals("") || resourceWorkCenter.getWorkCenterFk().equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("required column can not be empty!");
        } else {
            workCenterMapper.addWorkCenterRefResource(resourceWorkCenter);
            List list = workCenterMapper.selectResourceRefWorkCenter(resourceWorkCenter.getWorkCenterFk());
            jsonResult.setResult(list);
        }
        return jsonResult;
    }

}
